package com.arice.activity;

import java.util.List;

import com.arice.beans.ExpenseInfo;
import com.arice.beans.ExpensecodeInfo;
import com.arice.dao.ExpenseCodeDao;
import com.arice.dao.ExpenseDao;
import com.arice.util.DateHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ExpenseMainActivity extends Activity {
	// 需要适配的数据集合
	private List<ExpenseInfo> expenseList;
	// 数据库增删改查操作类
	private ExpenseDao dao;
	private ExpenseCodeDao codeDao;
	private ExpenseInfo expense;
	private MyAdapter adapter;
	// ListView
	private ListView ltExpense;
	private TextView tvMonthExpense;
	private Intent intent;
	private int userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expensemain);

		intent = getIntent();
		userId = intent.getIntExtra("userId", 0);
		dao = new ExpenseDao(this);
		expenseList = dao.getAllExpenseByuserId(userId);
		codeDao = new ExpenseCodeDao(this);

		adapter = new MyAdapter();
		// 初始化控件
		ltExpense = (ListView) findViewById(R.id.lvexpense);
		tvMonthExpense = (TextView) findViewById(R.id.tvmonthexpense);
		// 添加监听器，监听条目点击事件
		ltExpense.setOnItemClickListener(new MyOnItemClickListener());
		ltExpense.setAdapter(adapter);// 给ListView添加适配器(自动把数据生成条目)
		String date = "";
		double expenseMoney = 0.00,allExpense = 0.00;
		for (int i = 0; i < expenseList.size(); i++) {
			expense = expenseList.get(i);
			date = expense.getExpenseTime();
			allExpense +=expense.getExpenseMoney();
			String[] temp = date.split("-");
			if (temp[1].equals(DateHelper.getMonthByNow())) {
				expenseMoney += expense.getExpenseMoney();
			}
		}
		if (expenseMoney == 0.0) {
			tvMonthExpense.setText("您" + DateHelper.getMonthByNow()
					+ "月还没有记录哦，快来记一笔账吧.");
		} else {
			tvMonthExpense.setText("您当前总支出为"+allExpense+"元\r\n" + DateHelper.getMonthByNow() + "月的支出为"
					+ expenseMoney + "元");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.incomemainmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.action_addincome) {
			intent = new Intent(this, ExpenseAddActivity.class);
			intent.putExtra("userId", userId);
			startActivity(intent);
			this.finish();
		}
		return super.onOptionsItemSelected(item);
	}

	// 自定义一个适配器(把数据装到ListView的工具)
	private class MyAdapter extends BaseAdapter {

		public int getCount() { // 获取条目总数
			return expenseList.size();
		}

		public Object getItem(int position) { // 根据位置获取对象
			return expenseList.get(position);
		}

		public long getItemId(int position) { // 根据位置获取id
			return position;
		}

		// 获取一个条目视图
		public View getView(int position, View convertView, ViewGroup parent) {
			// 重用convertView
			View item = convertView != null ? convertView : View.inflate(
					ExpenseMainActivity.this, R.layout.expensemainitem, null);

			// 获取该视图中的TextView
			TextView tvIncomeAddtime = (TextView) item
					.findViewById(R.id.tvexpenseaddtime);
			TextView tvIncomeTypeandMoney = (TextView) item
					.findViewById(R.id.tvexpensetypeandmoney);
			// 根据当前位置获取Account对象
			expense = expenseList.get(position);
			String codeName = codeDao.getCodeNameByCodeId(expense
					.getExpenseCodeId());
			// 把IncomeInfo对象中的数据放到TextView中
			tvIncomeAddtime.setText(expense.getExpenseTime());
			tvIncomeTypeandMoney.setText(codeName + ":"+ expense.getExpenseMoney() + "¥");
			
			return item;
		}
	}

	private class MyOnItemClickListener implements OnItemClickListener {
		ExpenseInfo expense;
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// 获取点击位置上的数据
			expense = (ExpenseInfo) parent.getItemAtPosition(position);
			int codeid = expense.getExpenseCodeId();
			String codeName = codeDao.getCodeNameByCodeId(codeid);
			//点击listView每一项的条目点击事件
			AlertDialog.Builder builder = new Builder(ExpenseMainActivity.this);
			builder.setTitle("您想对此记录做什么操作呢？");
			builder.setItems(new String[]{"时间："+expense.getExpenseTime(),codeName+":"+expense.getExpenseMoney()+"","备注："+expense.getRemark()}, null);
			builder.setNeutralButton("删除", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					expenseList.remove(expense); // 从集合中删除
					dao.deleteByExpenseId(expense.getExpenseId()); // 从数据库中删除
					ExpenseMainActivity.this.adapter.notifyDataSetChanged();
				}
			});
			builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					intent = new Intent(ExpenseMainActivity.this, ExpenseAlterActivity.class);
					intent.putExtra("userId", userId);
					intent.putExtra("expenseId", expense.getExpenseId());
					intent.putExtra("expenseMoney", expense.getExpenseMoney());
					intent.putExtra("expenseTime", expense.getExpenseTime());
					intent.putExtra("remark", expense.getRemark());
					
					startActivity(intent);
					ExpenseMainActivity.this.finish();
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					dialog.dismiss();
				}
			});
			builder.show();
		}
		
		
	}
	
}
